﻿using SmallJob.Data;
using SmallJob.Data.Interfaces;
using SmallJob.Model;
using SmallJob.Services.Code;
using SmallJob.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SmallJob.Services.Implements
{
    public enum RequestFilter
    {
        General = 0,
        Location = 1,
        CreatedDate = 2,
        EndDate = 4,
        Price = 8
    }

    public class RequestService : RepositoryBase<Request>, IRequestService
    {
        public RequestService(IDatabaseFactory DBFactory)
            : base(DBFactory)
        {
        }

        public List<Request> GetByPage(int page, int pagesize, out int total)
        {
            var temp = DateTime.Now;
            var now = new DateTime(temp.Year, temp.Month, temp.Day, 0, 0, 0);
            var list = from r in SmallJobDB.Requests
                       where (r.EndDate >= now)
                       orderby r.CreatedDate descending
                       select r;
            int skip = (page - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }
        public List<Request> GetAllByPage(int page, int pagesize, out int total)
        {
            var list = from r in SmallJobDB.Requests
                       orderby r.CreatedDate descending
                       select r;
            int skip = (page - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }

        public List<Request> Find(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
                return SmallJobDB.Requests.OrderByDescending(r => r.CreatedDate).ToList();
            List<Request> result = new List<Request>();
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (var item in SmallJobDB.Requests)
            {
                int i = StringHanding.CountContain(s, item.Title);
                if (i > 0)
                {
                    dic.Add(item.ID, i);
                }
            }
            var sorted = dic.OrderByDescending(pair => pair.Value);
            foreach (var item in sorted)
            {
                result.Add(GetById(item.Key));
            }
            return result;
        }

        public List<Request> GetRequestByUserID(int id)
        {
            var list = from r in SmallJobDB.Requests
                       where r.AssignedUserID == id
                       orderby r.ModifiedDate descending
                       select r;
            return list.ToList();
        }

        public List<Request> GetRequestByCategoryID(int id)
        {
            var list = from r in SmallJobDB.Requests
                       where r.CategoryID == id
                       select r;
            return list.ToList();
        }

        public List<Request> GetByPageFilter(int indexpage, int pagesize, out int total, string valuefilter)
        {
            var temp = DateTime.Now;
            var now = new DateTime(temp.Year, temp.Month, temp.Day, 0, 0, 0);
            var list = from r in SmallJobDB.Requests
                       where r.Title.Contains(valuefilter) && (r.EndDate >= now)
                       orderby r.CreatedDate descending
                       select r;
            int skip = (indexpage - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }
        public List<Request> GetAllByPageFilter(int indexpage, int pagesize, out int total, string valuefilter)
        {
            var temp = DateTime.Now;
            var now = new DateTime(temp.Year, temp.Month, temp.Day, 0, 0, 0);
            var list = from r in SmallJobDB.Requests
                       where r.Title.Contains(valuefilter)
                       orderby r.CreatedDate descending
                       select r;
            int skip = (indexpage - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }

        public List<Request> GetByPageFilter(int indexpage, int pagesize, out int total, string location, int category, string title)
        {
            var temp = DateTime.Now;
            var now = new DateTime(temp.Year, temp.Month, temp.Day, 0, 0, 0);
            bool cklocation = string.IsNullOrWhiteSpace(location);
            bool cktitle = string.IsNullOrWhiteSpace(title);
            var list = from r in SmallJobDB.Requests
                       where (cklocation || r.Location.Contains(location)) &&
                       (category == 0 || r.CategoryID == category) &&
                       (r.EndDate >= now) &&
                       (cktitle || r.Title.Contains(title))
                       orderby r.CreatedDate descending
                       select r;
            int skip = (indexpage - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }

        public List<Request> GetForHome()
        {
            var temp = DateTime.Now;
            var now = new DateTime(temp.Year, temp.Month, temp.Day, 0, 0, 0);
            var list = from r in SmallJobDB.Requests
                       where (r.EndDate >= now)
                       orderby r.CreatedDate descending
                       select r;
            return list.Take(15).ToList();
        }
        public List<Request> GetForHome(int number)
        {
            var temp = DateTime.Now;
            var now = new DateTime(temp.Year, temp.Month, temp.Day, 0, 0, 0);
            var list = from r in SmallJobDB.Requests
                       where (r.EndDate >= now)
                       orderby r.CreatedDate descending
                       select r;
            return list.Take(number).ToList();
        }
    }
}